use crate::keyboards::unarchive_keyboard;
use crate::user::UserPost;

use anyhow::Result;

use teloxide::prelude2::*;
use uuid::Uuid;

pub async fn archive(bot: AutoSend<Bot>, q: CallbackQuery, data: &str) -> Result<()> {
    let user_id = q.from.id;
    let user = get_user(user_id).await?;
    archive_post(data, user.id).await?;
    let message_id = q.message.clone().unwrap().id;
    let chat_id = q.message.clone().unwrap().chat_id();
    bot.answer_callback_query(q.id).text("Archived!").await?;
    bot.edit_message_text(
        chat_id,
        message_id,
        format!("{} [Archived]", q.message.unwrap().text().unwrap()),
    )
    .await?;
    bot.edit_message_reply_markup(chat_id, message_id)
        .reply_markup(unarchive_keyboard(data))
        .await?;
    Ok(())
}
use crate::db::connect_to_db;
use crate::helpers::uuid_from_str;

use mongodb::bson::doc;

use super::get_user;
async fn archive_post(post_id: &str, user_id: Uuid) -> Result<()> {
    let bin_id = uuid_from_str(post_id)?;
    let db = connect_to_db().await?;
    let coll = db.collection::<UserPost>("userposts");
    let query = doc! {
        "user_id": user_id,
        "post_id": &bin_id
    };
    let update = doc! {
        "$set": {
            "read": true,
        }
    };
    /*let array_filters = vec![doc! {
        "elem.post_id": bin_id,
    }];*/
    /*let options = UpdateOptions::builder()
    .array_filters(array_filters)
    .build();*/
    coll.update_one(query, update, None).await?;
    Ok(())
}
